Public Class USB8255A
    Public Enum COMPORT
        COM1
        COM2
        COM3
        COM4
        COM5
        COM6
        COM7
        COM8
        COM9
    End Enum
    Public Structure DCB
        Public DCBlength As Int32
        Public BaudRate As Int32
        Public fBitFields As Int32 'See Comments in Win32API.Txt
        Public wReserved As Int16
        Public XonLim As Int16
        Public XoffLim As Int16
        Public ByteSize As Byte
        Public Parity As Byte
        Public StopBits As Byte
        Public XonChar As Byte
        Public XoffChar As Byte
        Public ErrorChar As Byte
        Public EofChar As Byte
        Public EvtChar As Byte
        Public wReserved1 As Int16 'Reserved; Do Not Use
    End Structure

    Public Structure COMMTIMEOUTS
        Public ReadIntervalTimeout As Int32
        Public ReadTotalTimeoutMultiplier As Int32
        Public ReadTotalTimeoutConstant As Int32
        Public WriteTotalTimeoutMultiplier As Int32
        Public WriteTotalTimeoutConstant As Int32
    End Structure

    'Declare constants.
    Public Const GENERIC_READ As Int32 = &H80000000
    Public Const GENERIC_WRITE As Int32 = &H40000000
    Public Const OPEN_EXISTING As Int32 = 3
    Public Const FILE_ATTRIBUTE_NORMAL As Int32 = &H80
    Public Const NOPARITY As Int32 = 0
    Public Const ONESTOPBIT As Int32 = 0

    'Declare references.
    Private Declare Auto Function CreateFile Lib "kernel32.dll" _
       (ByVal lpFileName As String, ByVal dwDesiredAccess As Int32, _
          ByVal dwShareMode As Int32, ByVal lpSecurityAttributes As IntPtr, _
             ByVal dwCreationDisposition As Int32, ByVal dwFlagsAndAttributes As Int32, _
                ByVal hTemplateFile As IntPtr) As IntPtr

    Private Declare Auto Function GetCommState Lib "kernel32.dll" (ByVal nCid As IntPtr, _
       ByRef lpDCB As DCB) As Boolean

    Private Declare Auto Function SetCommState Lib "kernel32.dll" (ByVal nCid As IntPtr, _
       ByRef lpDCB As DCB) As Boolean

    Private Declare Auto Function GetCommTimeouts Lib "kernel32.dll" (ByVal hFile As IntPtr, _
       ByRef lpCommTimeouts As COMMTIMEOUTS) As Boolean

    Private Declare Auto Function SetCommTimeouts Lib "kernel32.dll" (ByVal hFile As IntPtr, _
       ByRef lpCommTimeouts As COMMTIMEOUTS) As Boolean

    Private Declare Auto Function WriteFile Lib "kernel32.dll" (ByVal hFile As IntPtr, _
       ByVal lpBuffer As Byte(), ByVal nNumberOfBytesToWrite As Int32, _
          ByRef lpNumberOfBytesWritten As Int32, ByVal lpOverlapped As IntPtr) As Boolean

    Private Declare Auto Function ReadFile Lib "kernel32.dll" (ByVal hFile As IntPtr, _
       ByVal lpBuffer As Byte(), ByVal nNumberOfBytesToRead As Int32, _
          ByRef lpNumberOfBytesRead As Int32, ByVal lpOverlapped As IntPtr) As Boolean

    Private Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal hObject As IntPtr) As Boolean

    Private myHandle As IntPtr

    Public Sub Open(ByVal myCom As COMPORT)
        Dim conn As String
        conn = ""
        Select Case myCom
            Case COMPORT.COM1
                conn = "COM1"
            Case COMPORT.COM2
                conn = "COM2"
            Case COMPORT.COM3
                conn = "COM3"
            Case COMPORT.COM4
                conn = "COM4"
            Case COMPORT.COM5
                conn = "COM5"
            Case COMPORT.COM6
                conn = "COM6"
            Case COMPORT.COM7
                conn = "COM7"
            Case COMPORT.COM8
                conn = "COM8"
            Case COMPORT.COM9
                conn = "COM9"
        End Select
        myHandle = CreateFile(conn, GENERIC_READ Or GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero)
        If myHandle.ToInt32 = -1 Then
            MsgBox("Error in Opening COM6")
        End If
    End Sub
    Public Sub Close()
        CloseHandle(myHandle)
    End Sub
    Private Sub send(ByVal addr As Byte, ByVal data As Byte)
        Dim sendData(4) As Byte
        Dim BytesWritten As Int32
        sendData(0) = &HAA
        sendData(1) = &H3
        sendData(2) = &H1
        sendData(3) = Addr
        sendData(4) = data
        If Not WriteFile(myHandle, sendData, 5, BytesWritten, IntPtr.Zero) Then
            MsgBox("Error in sending data")
        End If
    End Sub
    Private Function receive(ByVal addr As Byte) As Byte
        Dim data(3) As Byte
        Dim data2(4) As Byte
        Dim BytesWritten As Int32
        Dim BytesRead As Int32
        data(0) = &HAA
        data(1) = &H1
        data(2) = &H2
        data(3) = addr
        If Not WriteFile(myHandle, data, 4, BytesWritten, IntPtr.Zero) Then
            MsgBox("Error in sending data")
        End If
        If Not ReadFile(myHandle, data2, 5, BytesRead, IntPtr.Zero) Then
            MsgBox("Error in receiving data")
        End If
        receive = data2(4)
    End Function
    Public Property U2PortA() As Byte
        Get
            U2PortA = receive(&H4)
        End Get
        Set(ByVal value As Byte)
            send(&H8, value)
        End Set
    End Property
    Public Property U3PortA() As Byte
        Get
            U3PortA = receive(&HC4)
        End Get
        Set(ByVal value As Byte)
            send(&HC8, value)
        End Set
    End Property
End Class
